package qq.android.wan.qq.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import qq.android.wan.qq.R;
import qq.android.wan.qq.utils.Constant;
import qq.android.wan.qq.utils.IntentUtil;

/**
 * Created by Administrator on 2015/12/10.
 */
public class LoginActivity extends Activity {

    private String account;
    private String password;
    private ProgressDialog progressDialog;
    private android.widget.Button btn_login;
    private android.widget.EditText et_account;
    private android.widget.EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_login);

        initUI();
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = et_account.getText().toString();
                password = et_password.getText().toString();
                if (account.length() == 0) {
                    Toast.makeText(getApplicationContext(), "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (password.length() == 0) {
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (new IntentUtil().isConnectionNet(LoginActivity.this)) {

                  //  clickLogin();
                    loginEM();
                } else {
                    Toast.makeText(LoginActivity.this, "网络不可用", Toast.LENGTH_SHORT).show();
                }

            }
        });

        // btn_login.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.AT_MOST);

    }
    public void clickLogin(){
        if(progressDialog==null)
        progressDialog=new ProgressDialog(LoginActivity.this);
        progressDialog.setMessage("登录中...");
        progressDialog.show();
        AsyncHttpClient client=new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("user.phone",account);
        params.put("user.password",password);
        String url= Constant.INTENT_ADDRESS+"User_login";
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                int state=Integer.parseInt(new String(bytes));
                if(state==Constant.ACCOUNT_FAILURE){
                    Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                }else if(state==Constant.ACCOUNT_NOEXIST){
                    Toast.makeText(LoginActivity.this,"账户不存在",Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                }else if(state==Constant.ACCOUNT_OK){
                    loginEM();
                    //Toast.makeText(LoginActivity.this,"登录错误",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Toast.makeText(LoginActivity.this,"登录错误",Toast.LENGTH_SHORT).show();
               progressDialog.dismiss();
            }
        });
    }
    /**
     * 将用户的信息保存在sp中
     * @param account 登录的账号
     * @param password 登录时的密码
     */
    private void saveInfo(String account, String password) {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("account", account);
        editor.putString("password", password);
        editor.commit();
    }
private void loginEM(){
    if(progressDialog==null)
        progressDialog=new ProgressDialog(LoginActivity.this);
    progressDialog.setMessage("登录中...");
    progressDialog.show();
    EMChatManager.getInstance().login(account, password, new EMCallBack() {//回调
        @Override
        public void onSuccess() {
            runOnUiThread(new Runnable() {
                public void run() {
                    EMGroupManager.getInstance().loadAllGroups();
                    EMChatManager.getInstance().loadAllConversations();
                    Log.e("test", "登陆聊天服务器成功！");
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    //把登录信息保存在sp中
                    saveInfo(account, password);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressDialog.dismiss();
                        }
                    });
                    finish();
                }
            });
        }

        @Override
        public void onProgress(int progress, String status) {

        }

        @Override
        public void onError(int code, String message) {

            Log.e("test", "登陆聊天服务器失败！");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(LoginActivity.this,"环信账号登录失败",Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                }
            });
        }
    });
}
    private void initUI() {
        this.et_password = (EditText) findViewById(R.id.et_password);
        this.et_account = (EditText) findViewById(R.id.et_account);
        btn_login = (Button) findViewById(R.id.btn_login);
    }
}
